﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RookieGrow.Domain.Model.SportsStore;
using RookieGrow.Data.SportsStore;
using RookieGrow.Infra.Cache;

namespace RookieGrow.Domain.SportsStore.Service
{
    public class ProductService
    {
        private IProductRepository productRepository;
        private ICacheStorage cacheStorage;

        public ProductService(IProductRepository productRepository, ICacheStorage cacheStorage)
        {
            this.productRepository = productRepository;
            this.cacheStorage = cacheStorage;
        }

        public List<Product> GetAllProducts()
        {
            string storageKey = "all_products";

            List<Product> products = cacheStorage.Retrieve<List<Product>>(storageKey);

            if (products == null)
            {
                products = productRepository.GetProducts();
                cacheStorage.Store(storageKey, products);
            }

            return products;
        }
    }
}
